#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int p = 0;
    int e = 0;
    int i = 0;
    int d = 0;
    int result = 0;
    int casees = 0;
    while(scanf("%d %d %d %d", &p, &e, &i, &d) == 4 && !(p == -1 && e == -1 && i == -1 && d == -1))
    {
        int step = 0;
        int tmp = (i%33) + 33*step;
        while(!((tmp%23) == (p%23) && (tmp%28) == (e%28) && tmp > d))
        {
            step++;
            tmp = (i%33) + 33*step;
        }
        result = tmp - d;
        casees++;
        printf("Case %d: the next triple peak occurs in %d days.\n", casees, result);
    }
    return 0;
}
